﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Talks
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Posts;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Messages;
    using System.Xml.Linq;
    public partial class PostThread : TalksPageBase
    {
        protected Guid _tid;
        protected Sc_Post mainPost;
        protected Sc_PostCategory _category;
        protected Sc_PostThread _thread;

        protected void Page_Init(object sender, EventArgs e)
        {
            Session["THREADPAGE"] = null;
            if (Request.QueryString["t"] == null)
            {
                RedirectToDefaultPage();
            }
            else
            {
                try
                {
                    _tid = new Guid(Request.QueryString["t"]);
                }
                catch { RedirectToDefaultPage(); }

                if (Request.QueryString["u"] != null)
                {
                    // update view count
                    PostOperations.UpdateViewCount(_tid);
                    //Sc_PostThread p = scDataContext.Instance.Sc_PostThreads.Single(d => d.ThreadId == _tid);
                    //p.ViewCount++;
                    RedirectToPostPage(_tid, false);
                }
                else
                {
                    _thread = PostOperations.GetThreadByThreadId(_tid);
                    mainPost = PostOperations.GetMainPostInThread(_tid);
                    _category = PostOperations.GetCategory(_thread.CategoryId);
                }
                this.SetMasterSiteMapOverride(true);
                
                

                FormView1.ItemInserting += new FormViewInsertEventHandler(FormView1_ItemInserting);
                FormView1.ItemInserted += new FormViewInsertedEventHandler(FormView1_ItemInserted);
                lvPosts.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvPosts_ItemCommand);
                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
            }
        }



        protected void Page_Load()
        {
            if (_thread == null)
            {
                RedirectToDefaultPage();
            }
            else
            {
                ltrTitle.Text = mainPost.PostTitle;
                ltrMainPostTitle.Text = mainPost.PostTitle;
                ltrMainPostTitle.ToolTip = mainPost.PostTitle;

                lblViewCount.Text = _thread.ViewCount.ToString();
                lblPostCount.Text = (_thread.TotalPosts - 1).ToString();
                ReadThreadOperations.AddReadHistoryToProfile(new ReadThreadEntry(_tid, mainPost.PostTitle, DateTime.Now));
            }
        }

        SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tmpNode = currentNode;
            tmpNode.Title = mainPost.PostTitle;

            tmpNode = currentNode.ParentNode;
            tmpNode.Title = _category.CategoryName;
            tmpNode.Url += "?c=" + _category.PostCategoryId;
            return currentNode;
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        void lvPosts_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeletePost":
                    PostOperations.MarkPostAsDeleted(new Guid(e.CommandArgument.ToString()));
                    break;
                default: break;
            }

            RefreshPage();
        }

        

        void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception == null)
            {
                if (mainPost.PostUserId != Profile.UserId)
                {
                    string eventName = "TalksReply";
                    // Send system message
                    XElement context = new XElement("TalksConext",
                        new XAttribute("XsltFolderInMod", "Talks"),
                        new XElement("Event",
                            new XAttribute("Name", eventName),
                            new XElement("PostTitle", mainPost.PostTitle),
                            new XElement("ReplierUserId", Profile.UserId),
                            new XElement("ThreadId", _tid)));
                    MessageOperations.SendSystemMessage(Profile.UserId, (Guid)mainPost.PostUserId, eventName, context, true);
                }
                RefreshPage(true);
            }
        }

        void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["PostId"] = Guid.NewGuid();
            e.Values["PostDate"] = DateTime.Now;
            e.Values["LastUpdateDate"] = DateTime.Now;
            e.Values["PostType"] = PostType.Original;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

        }

        protected void txtReplyTitle_Load(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = string.Format("回复: {0}", mainPost.PostTitle);
        }

        
    }
}
